<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['ischecklogin']=cfc('core')->checklogin('','',true);
if($_RQ['op']=='main'){
	$return['setting']=$this->plugin_setting('page_setting');
	$member=$this->get('agent',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$this->session('dian_id',$member['id']);
	$return['commission_money']=$this->sum('commission',array('mid'=>$_SESSION['uid']),'money');
	$return['commission_total']=$this->count('commission',array('mid'=>$_SESSION['uid']));
	$return['list']=array();
	$return['params']=array(
		'type'=>2,
		'status'=>1,
	);
	$return['psize']=20;
	exi($return);
}
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'dian_id'=>$this->session('dian_id'));
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	if($_RQ['status']){
		if($_RQ['status']==1){
			$params['status']=array(1,6,14,15,16,17,18,19,20);
		}else{
			$params['status']=intval($_RQ['status']);
		}
	}
	if(trim($_RQ['mobile'])){
		$params['mobile']=trim($_RQ['mobile']);
	}
	if($_RQ['type']){
		$params['sendtype']=intval($_RQ['type']);
	}
	//print_r($params);exit;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc'
	);
	$list=$this->get_list('order',$parameter);
	if(!$list){
		exi('none');
	}
	$work_weekarray=array('日','一','二','三','四','五','六');
	foreach($list as &$item){
		$item['member']=model('core/member')->basic($item['mid']);
		$order=pdo_get('jy_weishop_order',array('id'=>$item['orderid']),array('plugin_name','ordersn','realname','mobile','addressid','address'));
		if($order['plugin_name']=='jy_xiyi' && !$item['type']){
			$this->pdo_update('order',array('type'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['id']));
			$item['type']=1;
		}
		$address=$order['address']?json_decode($order['address'],true):$order['addressid'];
		if($address){
			$item['realname']=$address['name']?:$order['realname'];
			$item['addressinfo']=model('core/member')->get_address_str($address);
		}
		$item['ordersn']=$order['ordersn'];
		$address['name']=$address['name']?:$order['realname'];
		$address['mobile']=$address['mobile']?:$order['mobile'];
		if((!$item['realname'] || !$item['mobile']) && $address['mobile']){
			$item['realname']=$address['realname']?:$address['name'];
			$item['mobile']=$address['mobile'];
			$this->update('order',array('realname'=>$item['realname'],'mobile'=>$item['mobile']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['id']));
		}
		$item['address']=$address;
		$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$item['orderid'],'uniacid'=>$_SESSION['uniacid']),array('goodsid','istaocan','optionid','price','total','goodssn','productsn'));
		foreach($ordergoods as &$gv){
			$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','marketprice','unit','image','title','merchid'));
			$goodsinfo['total']=$gv['total'];
			$goodsinfo['price']=$gv['price'];
			if($_SESSION['is_tomedia']){
				$goodsinfo['image']=$goodsinfo['image']?tomedia($goodsinfo['image']):'';
			}
			$goodsinfo['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$gv['goodsid']),'weight');
			//商品规格
			if($gv['optionid']){
				$options=pdo_get('jy_weishop_goods_option',array('id'=>$gv['optionid']),array('title','specs'));
				if($gv['istaocan']){
					$goodsinfo['selespename']=$options['title'];
				}else{
					$specs=$options['specs'];
					$specs=explode("_",$specs);
					foreach($specs as &$spec){
						$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
					}
					$goodsinfo['selespename']=implode("，",$specs);
				}
			}
			$item['goods'][] = $goodsinfo;
		}
		if($item['sendtype']!=2){
			$item['statustitle']=$this->order_status[$item['status']];
		}else{
			$item['statustitle']=$this->order_status[$item['status']];
		}
		$week = date('w', strtotime($item['createtime']));
		$item['week'] = '星期'.$work_weekarray[$week];
		$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
		$selfinfo=pdo_get('jy_weishop_order_self',array('orderid'=>$item['id']));
		$selfinfo['selftime']=$selfinfo['selftime']?date('Y-m-d H:i:s',$selfinfo['selftime']):'尽快';
		$selfinfo['selftime']=$selfinfo['out_time']?:$selfinfo['selftime'];
		$selfinfo['number2']=substr($selfinfo['number'],-4);
		$item['selfinfo']=$selfinfo;
		$item['commission_money']=$this->pdo_getcolumn('commission',array('ordersn'=>$item['ordersn'],'uniacid'=>$_SESSION['uniacid']),'money');
		$item['commission_money']=$item['commission_money']?:'';
	}
	exi($list);
}
if($_RQ['op']=='tostatus2'){
	$logid=intval($_RQ['id']);
	if(!$logid){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->pdo_update('order',array('status'=>2),array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
	$this->model('notice')->notice_upstatus($logid,2);
	exi('操作成功!');
}
if($_RQ['op']=='tostatus3'){
	$logid=intval($_RQ['id']);
	if(!$logid){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->pdo_update('order',array('status'=>3),array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
	$orderid=$this->pdo_getcolumn('order',array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']),'orderid');
	model('jy_weishop/order')->order_receive($orderid);
	$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
	$this->model('commission')->create($order);
	$this->model('notice')->notice_upstatus($logid,3);
	exi('操作成功!');
}
if($_RQ['op']=='tostatus15'){
	$logid=intval($_RQ['id']);
	if(!$logid){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->pdo_update('order',array('status'=>15),array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
	$this->model('notice')->notice_upstatus($logid,15);
	exi('操作成功!');
}
if($_RQ['op']=='tostatus17'){
	$logid=intval($_RQ['id']);
	if(!$logid){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->pdo_update('order',array('status'=>17),array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
	$this->model('notice')->notice_upstatus($logid,17);
	exi('操作成功!');
}
if($_RQ['op']=='tostatus18'){
	$logid=intval($_RQ['id']);
	if(!$logid){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->pdo_update('order',array('status'=>18),array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
	$orderid=$this->pdo_getcolumn('order',array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']),'orderid');
	pdo_update('jy_weishop_order',array('status'=>2),array('id'=>$orderid,'uniacid'=>$_SESSION['uniacid']));
	$this->model('notice')->notice_upstatus($logid,18);
	exi('操作成功!');
}
if($_RQ['op']=='tostatus5'){
	$logid=intval($_RQ['id']);
	if(!$logid){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->pdo_update('order',array('status'=>5),array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
	$this->model('notice')->notice_upstatus($logid,5);
	exi('操作成功!');
}
if($_RQ['op']=='tostatus14'){
	$logid=intval($_RQ['id']);
	if(!$logid){
		exi('订单不存在，请重新选择订单','error');
	}
	$logorder=$this->pdo_get('order',array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
	$status=$logorder['status'];
	if($status==15){
		$this->pdo_update('order',array('status'=>14,'dian_id'=>0),array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$this->pdo_update('order',array('status'=>0,'dian_id'=>0),array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
	}
	$order=pdo_get('jy_weishop_order',array('id'=>$logorder['orderid'],'uniacid'=>$_SESSION['uniacid']),array('api_data'));
	$order['api_data']=json_decode($order['api_data'],true);
	$order['api_data']['area_dianid']=0;
	pdo_update('jy_weishop_order',array('api_data'=>ijson_encode($order['api_data'])),array('id'=>$logorder['orderid'],'uniacid'=>$_SESSION['uniacid']));
	$this->model('notice')->notice_upstatus($logid,14);
	exi('转单成功!');
}
?>